package com.voodoo.lib_mqtt.listener;

import com.voodoo.lib_logcat.L;
import com.voodoo.lib_mqtt.MqttOperation;

import org.eclipse.paho.client.mqttv3.IMqttDeliveryToken;
import org.eclipse.paho.client.mqttv3.MqttCallbackExtended;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;

/**
 * Author：voodoo
 * CreateTime：2021/07/15 015 下午 05:06
 * Description：MQTT的监听接口以及链接信息
 */
public class MqttCallbackListener implements MqttCallbackExtended {

    @Override
    public void connectComplete(boolean reconnect, String serverURI) {
        L.i("与服务器的连接成功完成...");
    }

    @Override
    public void connectionLost(Throwable exception) {
        L.e("Mqtt与服务器的连接丢失...", exception);
    }

    @Override
    public void messageArrived(String topic, MqttMessage message) throws Exception {
        String body = new String(message.getPayload());
//        L.i("Mqtt收到服务器消息", "主题名称：" + topic, "接收消息：" + body);
        MqttOperation.getInstance().processMsg(topic, body); // 处理回传的消息
    }

    @Override
    public void deliveryComplete(IMqttDeliveryToken token) {
        try {
            L.i("Mqtt交互完成", token.getMessage());
        } catch (MqttException e) {
            L.i("Mqtt交互异常");
        }
    }

}
